#!/usr/bin/python2.7
# coding:utf-8
import commands


class MysqlMS():
    master_shell = {
        "creat_slave": "CREATE USER 'slave'@'%' IDENTIFIED BY '123456';",
        "replication": "GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';",
        "show_status": "show master status;",
    }
    master_docker_exec = 'docker exec -it mysql_master '
    slave_docker_exec = 'docker exec -it mysql_slave '
    
    def __init__(self, user="root", password="123456"):
        self.mysql_login_shell = "mysql -u{} -p{} -e ".format(user, password)
        self.master_run()
        self.slave_run()
        self.redis_run()
    def master_run(self):
        '''在mysql主容器中执行配置命令，返回包含bin_file和position字典
        '''
        commands.getstatusoutput(
            self.master_docker_exec +self.mysql_login_shell + '"' + "drop user 'slave'@'%';" + '"'
        )
        commands.getstatusoutput(
            self.master_docker_exec +self.mysql_login_shell + '"' + self.master_shell['creat_slave'] + '"'
        )
        commands.getstatusoutput(
             self.master_docker_exec + self.mysql_login_shell + '"' + self.master_shell['replication'] + '"'
        )
        master_status = commands.getstatusoutput(
           self.master_docker_exec + self.mysql_login_shell + '"' +  self.master_shell['show_status'] + '"' + "|grep mysql-bin"
        )
        print(master_status)
        info = master_status[1].replace(' ', '')
        info = info.split('|')
        self.bin_file = info[1]
        self.position = info[2] 
        return {"bin_file": info[1], "position": info[2]}

    def slave_run(self):
        slave_run_shell = """CHANGE MASTER TO MASTER_HOST='mysql_master',
            MASTER_PORT=3306,\
            MASTER_USER='slave',\
            MASTER_PASSWORD='123456',\
            MASTER_LOG_FILE='{}',\
            MASTER_LOG_POS={};""".format(self.bin_file, self.position)
        commands.getstatusoutput(
            self.slave_docker_exec + self.mysql_login_shell + '"' + "stop slave;" + '"'
        )
        commands.getstatusoutput(
            self.slave_docker_exec + self.mysql_login_shell + '"' + slave_run_shell + '"'   
        )
        commands.getstatusoutput(
            self.slave_docker_exec + self.mysql_login_shell + '"' + "start slave;" + '"'
        )
        info = commands.getstatusoutput(
            self.slave_docker_exec + self.mysql_login_shell + '"' + "show slave status \G" + '"'
        )

        print(info)
    
    def redis_run(self):
        commands.getstatusoutput(
            "docker exec -it redis redis-cli config set requirepass 123456"
        )
        commands.getstatusoutput(
            "docker exec -it redis redis-cli -a 123456 CONFIG SET maxmemory 1500mb"
        )
        commands.getstatusoutput(
            "docker exec -it redis redis-cli CONFIG SET maxmemory-policy allkeys-lru"
        )


     
if __name__ == '__main__':
    import time
    # commands.getstatusoutput("docker rm -f redis ")
    commands.getstatusoutput("docker-compose up -d")
    time.sleep(20)
    x = MysqlMS()
    














   
        

